

//分页
export default {
	data: function() {
		return {
			page: {
				pageNo: 1, //当前页码
				pageSize: 10, //显示条数
				total: 0, //总条数
			},
			req: {}, // search时拷贝查询表单
      // 表格选中项
      selection: [], 
		}
	},
  mounted () {
    this.search();
  },
	methods: {
		//查询数据
		search(type) {
			if (this.form) {
				this.req = JSON.parse(JSON.stringify(this.form));
			}
			if(type == 1){//如果类型为1时分页设置默认值
				this.page.pageSize = 10;
			}
			this.loadData();
		},

    // 获取数据、如果有不一样的可以通过重置loadData事件，来改变
    loadData () {
      // 如果需要转义form表单，自己添加一个formatForm事件
      var param = this.formatForm ? this.formatForm() : this.req;
      param.pageNo = this.page.pageNo;
      param.pageSize = this.page.pageSize;

      if (!this.url.list) {
        console.log('请设置url.list属性！');
        return;
      }
      this.loadInterface = 1; // 设置加载接口数量为1
      this.$get(this.url.list, param).then(res => {
        var result = res.result;
        this.table.data = result.records;
        this.page.total = result.total;
      })
    },

    // 批量删除
    handleBatchDelete (key = 'id', name = 'table') {
      var arr = this.selection.map(item => {
        return item[key];
      })
      this.handleDelete(arr);
    },

		// 删除数据，单个删除id为id，多个删除id为数组
		handleDelete (id) {
      var msg = '', text = '', param = {}, url = '';
      if (typeof id == 'object') {
        text = '选中数据';
        param.ids = id.join(',');
        url = this.url.deleteBatch;
        if (!id || !id.length) { msg = '请选择一条记录！'; }
        if (!url) { msg = '请设置url.deleteBatch属性！'; }
      } else {
        text = '该条数据';
        param.id = id;
        url = this.url.delete;
        if (!url) { msg = '请设置url.delete属性！'; }
      }
      if (msg) {
        this.$message.error(msg);
        return;
      }
      this.$g.confirm('此操作将删除' + text + ', 是否继续?').then(() => {
        this.$postDelete(url, param).then(res => {
          if (res.success) {
            this.$message.success(res.message);
            this.loadData();
          }
        })
      })
		},

    // 表格选中改变
    handleSelectionChange (arr) {
      this.selection = arr;
    },

    // 清除选择项
    handleClearSelection (name = 'table') {
      this.$refs[name].clearSelection();
    },

    changePage(val, state) {
      if(state) { //修改显示条数
        this.page.pageSize = val;
        this.page.pageNo = 1;
      } else { //页码改变
        this.page.pageNo = val;
      }
      this.loadData();
    },

	},
}